package com.aibaixun.system.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import com.aibaixun.system.domain.EquipmentManage;
import com.aibaixun.system.domain.PostureDevice;
import com.aibaixun.system.domain.dto.UpdateEquipmentManageDto;
import com.aibaixun.system.domain.vo.ExportEquipmentManageVo;
import com.aibaixun.system.mapper.EquipmentManageMapper;
import com.aibaixun.system.service.EquipmentManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.poi.excel.ExcelUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b>EquipmentManageServiceImpl</b>
 *
 * @description: EquipmentManageServiceImpl <br>
 * @date: 2024/7/23 16:12 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Service
public class EquipmentManageServiceImpl extends ServiceImpl<EquipmentManageMapper, EquipmentManage> implements EquipmentManageService {
    private static final Logger logger = LoggerFactory.getLogger(EquipmentManageServiceImpl.class);

    @Override
    public void uploadEquipmentManage(MultipartFile file) {
        List<EquipmentManage> equipmentManageList = new ArrayList<>();
        try {
            InputStream ins = file.getInputStream();
            ExcelReader reader = ExcelUtil.getReader(ins);
            List<Map<String, Object>> maps = reader.readAll();
            // 从第三行开始读取
            AtomicInteger i = new AtomicInteger();
            maps.forEach(column -> {
                EquipmentManage equipmentManage = new EquipmentManage();
                if (!String.valueOf(column.get("配线编号")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("配线编号")))) {
                    equipmentManage.setWiringNo(String.valueOf(column.get("配线编号")));
                }
                if (!String.valueOf(column.get("所属机房")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("所属机房")))) {
                    equipmentManage.setComputerRoomName(String.valueOf(column.get("所属机房")));
                }
                if (!String.valueOf(column.get("P1端设备编号")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("P1端设备编号")))) {
                    equipmentManage.setEquipmentNoP1(String.valueOf(column.get("P1端设备编号")));
                }
                if (!String.valueOf(column.get("P2端设备编号")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("P2端设备编号")))) {
                    equipmentManage.setEquipmentNoP2(String.valueOf(column.get("P2端设备编号")));
                }
                if (!String.valueOf(column.get("P1端设备名称")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("P1端设备名称")))) {
                    equipmentManage.setEquipmentNameNoP1(String.valueOf(column.get("P1端设备名称")));
                }
                if (!String.valueOf(column.get("P2端设备名称")).equals("null") && StringUtils.isNotBlank(String.valueOf(column.get("P2端设备名称")))) {
                    equipmentManage.setEquipmentNameNoP2(String.valueOf(column.get("P2端设备名称")));
                }
                equipmentManage.setEnable("1");
                equipmentManage.setCreateTime(new Date());
                equipmentManageList.add(equipmentManage);
            });
        }catch (IOException e){

        }catch (Exception e){

        }

        this.saveBatch(equipmentManageList);

    }

    @Override
    public List<ExportEquipmentManageVo> exportEquipmentManage() {
        return baseMapper.exportEquipmentManage();
    }

    @Override
    public boolean createEquipmentManage(ExportEquipmentManageVo dto) {
        EquipmentManage equipmentManage = new EquipmentManage();
        equipmentManage.setWiringNo(dto.getWiringNo());
        equipmentManage.setComputerRoomName(dto.getComputerRoomName());
        equipmentManage.setEquipmentNoP1(dto.getEquipmentNoP1());
        equipmentManage.setEquipmentNoP2(dto.getEquipmentNoP2());
        equipmentManage.setEquipmentNameNoP1(dto.getEquipmentNameNoP1());
        equipmentManage.setEquipmentNameNoP2(dto.getEquipmentNameNoP2());
        equipmentManage.setEnable("1");
        equipmentManage.setCreateTime(new Date());
        int insert = baseMapper.insert(equipmentManage);
        if (1 == insert){
            return true;
        }
        return false;
    }

    @Override
    public IPage<EquipmentManage> queryEquipmentManageList(Integer pageNum,Integer pageSize,String wiringNo) {
        Page<EquipmentManage> page = new Page<>(pageNum, pageSize);
        return baseMapper.queryEquipmentManageList(page,wiringNo);
    }

    @Override
    public boolean updateEquipmentManage(UpdateEquipmentManageDto dto) {
        EquipmentManage equipmentManage = new EquipmentManage();
        equipmentManage.setId(dto.getId());
        equipmentManage.setWiringNo(dto.getWiringNo());
        equipmentManage.setComputerRoomName(dto.getComputerRoomName());
        equipmentManage.setEquipmentNoP2(dto.getEquipmentNoP2());
        equipmentManage.setEquipmentNoP1(dto.getEquipmentNoP1());
        equipmentManage.setEquipmentNameNoP1(dto.getEquipmentNameNoP1());
        equipmentManage.setEquipmentNameNoP2(dto.getEquipmentNameNoP2());
        equipmentManage.setEnable("1");
        equipmentManage.setCreateTime(new Date());
        int i = baseMapper.updateById(equipmentManage);
        if (1 == i){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteEquipmentManage(Long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id",id);
        updateWrapper.set("enable","0");
        int update = baseMapper.update(null, updateWrapper);
        if (1 == update){
            return true;
        }
        return false;
    }
}
